package com.yunkaweilai.android.function.update;

import android.app.Activity;
import android.app.Dialog;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.yunkaweilai.android.R;

import org.lzh.framework.updatepluginlib.creator.DialogCreator;
import org.lzh.framework.updatepluginlib.model.Update;
import org.lzh.framework.updatepluginlib.util.SafeDialogOper;

public class CustomUpdateCreatorWjx extends DialogCreator {
    @Override
    public Dialog create(final Update update, Activity activity) {

        if (activity == null || activity.isFinishing()) {
            Log.e("DialogCreator--->", "Activity was recycled or finished,dialog shown failed!");
            return null;
        }


        final MaterialDialog dialog = new MaterialDialog.Builder(activity)
                .customView(R.layout.dialog_update_app, false).cancelable(false)
                .build();
        View customView = dialog.getCustomView();
        TextView idTvAppVersion = (TextView) customView.findViewById(R.id.id_tv_app_version);
        TextView idTvUpdateContent = (TextView) customView.findViewById(R.id.id_tv_update_content);
        idTvAppVersion.setText("版本号：" + update.getVersionName());
        idTvUpdateContent.setText(Html.fromHtml(update.getUpdateContent()));
        Button idBtnUpdate = (Button) customView.findViewById(R.id.id_btn_update);
        TextView idTvDisUpdate = (TextView) customView.findViewById(R.id.id_tx_disupdate);
        idBtnUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //立即更新
                sendDownloadRequest(update);
                SafeDialogOper.safeDismissDialog(dialog);
            }
        });
        idTvDisUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //暂不更新
                sendUserCancel();
                SafeDialogOper.safeDismissDialog(dialog);
            }
        });
        if (update.isForced()){
            idTvDisUpdate.setVisibility(View.GONE);
        }else{
            idTvDisUpdate.setVisibility(View.VISIBLE);
        }

        return dialog;
    }
}
